<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Polls extends CI_Controller {
	var $perpage = 20;

		public function __construct() {
		   parent::__construct();
		   if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) redirect('auth/login/'.str_replace("/", ".",$_SERVER['REQUEST_URI']));
		   $this->layout->setlayout('admin/layout');
		   $this->crumps->push('/admin/polls/', 'Голосования');
		   $this->users_rights_model->check_rights(strtolower(get_class()), $this->router->get_method());
	   }

	public function index($id = 0)	{
		$data['pages'] = $pages = $this->db->select()->from('polls')->order_by('poll_id', 'desc')->get()->result_array();
		$form = new jQuickForm('simple');
		$random = md5(time()).md5(md5(time()));

		$form->addElement('hidden', 'random')->setValue($random);
		$form->setLabel("Добавление голосования");
		$form->insertInputText('poll_title')->setLabel('Название голосования, вопрос который Вы задаете')->addRuleRequired();
//		$form->insertFCKeditor('poll_text')->setLabel('Текст описание')->setComment('Не обязательно')->setHeight('150');
//		$post_params = array(
//			'random' => $random,
//			'session' => $this->session->userdata('session_id'),
//			'middle_required' => true,
//			'small_required' => true,
//			'middle_type' => 'min',
//			'small_type' => 'min',
//			'max_middle_height' => 360,
//			'max_middle_width' => 270,
//			'max_small_height' => 210,
//			'max_small_width' => 165
//		);
//		$form->insertMultiUpload('poll_image', '/admin/tools/upload/news/', $post_params, 1, false, 2048000)->setId('poll_image_0')->setLabel('Фотография для голосования')->setComment('Максимум только одна фотография (jpg/png/gif, 1024Kb, 1000*1000px)');

		$gr = $form->insertGroup()->addClass("col col2")->setLabel('Варианты ответа');
		$gr->insertInputText('answer[]');
		$gr->insertInputText('answer[]');
		$gr->insertInputText('answer[]');
		$gr->insertInputText('answer[]');
		$gr->insertInputText('answer[]');
		$gr->insertInputText('answer[]');

		if ($_SERVER['REQUEST_METHOD'] == "POST" && $form->validate()) {
			$p_data = $this->input->post();
			$in_data['poll_id'] = 0;
			$in_data['poll_title'] = $p_data['poll_title'];
			$in_data['poll_text'] = $p_data['poll_text'];
			$in_data['poll_count'] = 0;
			$this->db->insert('polls', $in_data);
			$o = $this->db->insert_id();
			$files = $this->tools_model->copyfiles($p_data['random'], $o, 'polls');
			if (sizeof($files) > 0) $this->db->update('polls', array('poll_image' => $files[0]), array('poll_id' => $o));
			if (sizeof($p_data['answer']) > 0) {
				foreach ($p_data['answer'] as $answer) {
					if ($answer != "") {
						$o_data['answer_id'] = 0;
						$o_data['answer_text'] = $answer;
						$o_data['answer_poll'] = $o;
						$o_data['answer_count'] = 0;
						$this->db->insert('answers', $o_data);
					}
				}
			}
			$this->session->set_flashdata('msg', 'Успешно добавлено');
			redirect('admin/polls');
		}

		$form->insertInputSubmit("Добавить");

		$data['form'] = $form."";
		$data['js'] = jQuickForm::js()->getJsAll();

		$this->layout->view('admin/polls/index', $data);
	}
	public function delete($id = 0) {
		if ($id != 0) $data['page'] = $page = $this->db->select()->from('polls')->where('poll_id', $id)->get()->result_array();
		if ($id != 0 && sizeof($page) <= 0) redirect('admin/polls');
		$this->db->where('poll_id', $id)->delete('polls');
		$this->db->where('answer_poll', $id)->delete('answers');
		$this->session->set_flashdata('msg', 'Успешно удалено');
		redirect('admin/polls');
	}
}